package util
{
	import flash.external.ExternalInterface;
	import model.Layer;
	import model.LayerAction;
	import model.DataLayer;
	import mx.controls.Alert;
	
	/**
	 * Utilities 类
	 * @Author liupj
	 */ 
	public class Util
	{
		/*object转换为图层对象*/		
		public static function setLayerProperties(layer : Layer, obj : Object) : void
		{
			layer.layerId = obj.layerId;
			layer.picPath = obj.picPath;
			layer.fullArea = obj.fullArea;
			
			if (obj.color != null) {
				layer.color = parseInt("0x" + obj.color);
			} else {
				layer.color = -1;
			}
			
			layer.layerName = obj.layerName;
			layer.pageId = obj.pageId;
			layer.preLoadFlag = obj.preLoadFlag;
			layer.transparency = obj.transparency/100;
			layer.sorting = obj.sorting;
			layer.baseMapType = obj.baseMapType;
			layer.layerType = obj.layerType;
			
			layer.width = obj.width;
			layer.height = obj.height;
			layer.xPos = obj.xPos;
			layer.yPos = obj.yPos;
			
			//设置图层动作列表
			var layerActionList : Array = new Array();
			
			if (obj.layerActionlst != null && obj.layerActionlst.length > 0) {
				var layerAction : LayerAction;
				
				for (var index : Number = 0; index < obj.layerActionlst.length; index ++) {
					if (obj.layerActionlst[index] != null) {
						layerAction = new LayerAction(obj.layerActionlst[index].actionId,
							obj.layerActionlst[index].type,
							obj.layerActionlst[index].triggerCond,
							obj.layerActionlst[index].effect);
						
						layerActionList.push(layerAction);
					}
				}
			}
			
			layer.layerActionList = layerActionList;
			
			
			
			
			//设置数据图层列表
//			var dataLayerlst : Array = new Array();
//			
//			if (obj.dataLayerlst != null && obj.dataLayerlst.length > 0) {
//				var dataLayer : DataLayer;
//				
//				for (var index : Number = 0; index < obj.layerActionlst.length; index ++) {
//					if (obj.layerActionlst[index] != null) {
//						layerAction = new LayerAction(obj.layerActionlst[index].actionId,
//							obj.layerActionlst[index].type,
//							obj.layerActionlst[index].triggerCond,
//							obj.layerActionlst[index].effect);
//						
//						layerActionList.push(layerAction);
//					}
//				}
//			}
//			
//			layer.layerActionList = layerActionList;
		}
		
		/*根据动作效果，返回不同的持续时间（时间值来源于外部JS的定义）*/
		public static function getDuration(type:String, layer : Layer) : int {
			var layerAction : LayerAction  = getProperLayerAction(layer, type);
			
			if (Const.ACTION_EFFECT_FAST == layerAction.effect) {
				return ExternalInterface.call("getEffectTime", Const.ACTION_EFFECT_FAST);
				
			} else if (Const.ACTION_EFFECT_SLOW == layerAction.effect) {
				return ExternalInterface.call("getEffectTime", Const.ACTION_EFFECT_SLOW);
				
			} else if (Const.ACTION_EFFECT_VERYSLOW == layerAction.effect) {
				return ExternalInterface.call("getEffectTime", Const.ACTION_EFFECT_VERYSLOW);
			}
			
			return -1;
		}
		
		
		private static function getProperLayerAction(layer : Layer, type:String) : LayerAction {
			for (var index : Number = 0; index < layer.layerActionList.length; index ++) {
				
				if (layer.layerActionList[index].type == type) {
					return layer.layerActionList[index];
				}
			}
			
			return null;
		}
		
		public static function containsType(type : String, layer : Layer) : Boolean {
			var layerActionList : Array = layer.layerActionList;
			
			for (var index : Number = 0; index < layerActionList.length; index ++) {
				var item : LayerAction = layerActionList[index];
				
				if (item.type == type) {
					return true;
				}
			}
			
			return false;
		}
	}
}